# -*- coding: utf-8 -*-
import scipy
import scipy.cluster.hierarchy as sch
from scipy.cluster.vq import vq, kmeans, whiten
import numpy as np
import matplotlib.pylab as plt


# 待聚类的数据点,cancer.csv有653行数据,每行数据有11维:
dataset = np.loadtxt('数据集.csv', delimiter=",")
# np数据从0开始计算
points = dataset[:, 0:10]
cancer_label = dataset[:, 11]
print("points:\n", points)
print("cancer_label:\n", cancer_label)
# k-means聚类
# 将原始数据做归一化处理
data = whiten(points)
# 使用kmeans函数进行聚类,输入第一维为数据,第二维为聚类个数k.
# k-means最后输出的结果其实是两维的,第一维是聚类中心,第二维是损失distortion,我们在这里只取第一维,所以最后有个[0]
# centroid = kmeans(data,max(cluster))[0]
centroid = kmeans(data, 4)[0]
print("cnetroid:\n",centroid)
# 使用vq函数根据聚类中心对所有数据进行分类,vq的输出也是两维的,[0]表示的是所有数据的label
label = vq(data, centroid)[0]
num = [0, 0]
for i in label:
    if (i == 0):
        num[0] = num[0] + 1
    else:
        num[1] = num[1] + 1
print('num =', num)
# np.savetxt('file.csv',label)
print("Final clustering by k-means:\n", label)
result = np.subtract(label, cancer_label)
print("result:\n", result)

count = [0, 0]
for i in result:
    if (i == 0):
        count[0] = count[0] + 1
    else:
        count[1] = count[1] + 1
print(count)
print(float(count[1]) / (count[0] + count[1]))





import pandas as pd
from pyecharts.charts import  Line,Calendar,Scatter
from pyecharts import  options as opts
from pyecharts.options import InitOpts,TitleOpts,LegendOpts,ToolboxOpts,VisualMapOpts,AxisOpts

data_1=pd.read_excel("副本Problem_C_Data_Wordle.xlsx")
label_2=list(data_1["label_3"])
number=list(data_1["number"])
list=[list(z)for z in zip (number,label_2)]

line1=Scatter(init_opts=InitOpts(width="1200px",height="300px",theme="white",))
line1.add_xaxis(number)
line1.add_yaxis(series_name="classification",y_axis=label_2)

line1.set_global_opts(
    title_opts=TitleOpts("Word classification after processing by clustering", pos_left="end", pos_top="1%"),
    legend_opts=LegendOpts(is_show=True),
    toolbox_opts=ToolboxOpts(is_show=True),
    visualmap_opts=VisualMapOpts(is_show=True,min_=0,max_=3),
    xaxis_opts=AxisOpts(name="number",is_inverse=False,name_location="end"),
)

line1.set_series_opts(
    label_opts=opts.LabelOpts(is_show=False)
)

line1.render("聚类分析折线图.html")






